package cn.kgc.intercepter;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @描述
 * @author：LY
 * @2018/11/7 18:01
 */
public class Intercepter implements HandlerInterceptor /*extends HandlerInterceptorAdapter*/ {



    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {

        String path = httpServletRequest.getRequestURI();
        if(path.contains("login") || path.equals("/")){
            System.out.println("登录要放行");
            return  true;
        }

        if(null==httpServletRequest.getSession().getAttribute("userLogin")){
            System.out.println("null");
            httpServletResponse.sendRedirect("/");
            return false;
        }

        return true;

    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
